using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;

namespace CustomChart
{
	public partial class AxisZoomWindow : Window, IComponentConnector
	{
		private const int Ratio = 100;

		private const double IncrementRatio = 0.02;

		private BaseChart _Chart;

		public BaseChart Chart
		{
			private get
			{
				return _Chart;
			}
			set
			{
				_Chart = value;
				if (_Chart != null)
				{
					double num = 1.0;
					int num2 = 1;
					if (_Chart is TimeChart)
					{
						nsMinX.Visibility = Visibility.Collapsed;
						nsMaxX.Visibility = Visibility.Collapsed;
						timeMinX.Visibility = Visibility.Visible;
						timeMaxX.Visibility = Visibility.Visible;
						timeMinX.Value = _Chart.MinX;
						timeMinX.setDate(new DateTime(1, 1, 1, 0, 0, 0).AddSeconds(_Chart.MinX).ToLongTimeString());
						timeMaxX.Value = _Chart.MaxX;
						timeMaxX.setDate(new DateTime(1, 1, 1, 0, 0, 0).AddSeconds(_Chart.MaxX).ToLongTimeString());
						DateTime dateTime = new DateTime(1, 1, 1, 23, 59, 59);
						DateTime dateTime2 = new DateTime(1, 1, 1);
						timeMinX.MaxValue = (dateTime - dateTime2).TotalSeconds;
						timeMinX.MinValue = 0.0;
						timeMaxX.MinValue = timeMinX.MinValue;
						timeMaxX.MaxValue = timeMinX.MaxValue;
					}
					else
					{
						nsMinX.MinValue = _Chart.MinX;
						nsMinX.MaxValue = _Chart.MaxX;
						nsMaxX.MinValue = nsMinX.MinValue;
						nsMaxX.MaxValue = nsMinX.MaxValue;
						nsMinX.StringFormat = _Chart.GetXFormat();
						nsMaxX.StringFormat = _Chart.GetXFormat();
						num = 1.0;
						nsMinX.Increment = num;
						nsMaxX.Increment = num;
					}
					nsMinY.StringFormat = _Chart.GetYFormat();
					nsMaxY.StringFormat = _Chart.GetYFormat();
					num = 1.0;
					num2 = nsMinY.StringFormat.IndexOf(".");
					if (num2 != -1)
					{
						num = Math.Pow(10.0, -(nsMinY.StringFormat.Length - num2 - 1));
					}
					nsMinY.Increment = num * 100.0;
					nsMaxY.Increment = num * 100.0;
					nsMaxX.Value = _Chart.GetCurrentMaxX();
					nsMinX.Value = _Chart.GetCurrentMinX();
					nsMaxY.Value = _Chart.GetCurrentMaxY();
					nsMinY.Value = _Chart.GetCurrentMinY();
					setNsYIncrement();
				}
			}
		}

		private void setNsYIncrement()
		{
			double num = 1.0;
			int num2 = nsMinY.StringFormat.IndexOf(".");
			if (num2 != -1)
			{
				num = Math.Pow(10.0, -(nsMinY.StringFormat.Length - num2 - 1));
			}
			double result = 1.0;
			if (double.TryParse(string.Format("{0:N2}", (nsMaxY.Value - nsMinY.Value) * 0.02).ToString(), out result) && result > num)
			{
				num = result;
			}
			nsMinY.Increment = num;
			nsMaxY.Increment = num;
		}

		public AxisZoomWindow()
		{
			InitializeComponent();
		}

		public void viewChangeByLinearType()
		{
			if (!_Chart.IsAxisYLinear())
			{
				nsMinY.Visibility = Visibility.Collapsed;
				nsMaxY.Visibility = Visibility.Collapsed;
				logMaxY.Visibility = Visibility.Visible;
				logMinY.Visibility = Visibility.Visible;
				setComBoxContent();
				logMaxY.SelectedValue = _Chart.GetCurrentMaxY();
				logMinY.SelectedValue = _Chart.GetCurrentMinY();
			}
		}

		private void setComBoxContent()
		{
			Dictionary<string, double> dictionary = new Dictionary<string, double>();
			for (double num = _Chart.MinY; num < _Chart.GetCurrentMaxY(); num *= 10.0)
			{
				dictionary.Add(num.ToString("0.#####"), num);
			}
			logMinY.ItemsSource = dictionary;
			Dictionary<string, double> dictionary2 = new Dictionary<string, double>();
			for (double num = _Chart.MaxY; num > _Chart.GetCurrentMinY(); num /= 10.0)
			{
				dictionary2.Add(num.ToString("0.#####"), num);
			}
			logMaxY.ItemsSource = dictionary2;
		}

		private void btnDefault_Click(object sender, RoutedEventArgs e)
		{
			if (Chart != null)
			{
				Chart.RestoreAxisZoom();
				nsMaxX.Value = _Chart.GetCurrentMaxX();
				nsMinX.Value = _Chart.GetCurrentMinX();
				if (!Chart.IsAxisYLinear())
				{
					setComBoxContent();
					logMaxY.SelectedValue = _Chart.GetCurrentMaxY();
					logMinY.SelectedValue = _Chart.GetCurrentMinY();
				}
				if (_Chart is TimeChart)
				{
					timeMaxX.Value = _Chart.GetCurrentMaxX();
					timeMinX.Value = _Chart.GetCurrentMinX();
					timeMaxX.setDate(new DateTime(1, 1, 1, 0, 0, 0).AddSeconds(_Chart.MaxX).ToLongTimeString());
					timeMinX.setDate(new DateTime(1, 1, 1, 0, 0, 0).AddSeconds(_Chart.MinX).ToLongTimeString());
				}
				nsMaxY.Value = _Chart.GetCurrentMaxY();
				nsMinY.Value = _Chart.GetCurrentMinY();
			}
		}

		private void btnApply_Click(object sender, RoutedEventArgs e)
		{
			if (Chart == null)
			{
				return;
			}
			if (_Chart is TimeChart)
			{
				if (Chart.GetCurrentMinX() != timeMinX.Value)
				{
					Chart.SetMinX(timeMinX.Value);
					Chart.IsCursomAxisValue = true;
				}
				if (Chart.GetCurrentMaxX() != timeMaxX.Value)
				{
					Chart.SetMaxX(timeMaxX.Value);
					Chart.IsCursomAxisValue = true;
				}
			}
			else
			{
				if (Chart.GetCurrentMinX().ToString(nsMinX.StringFormat) != nsMinX.Value.ToString(nsMinX.StringFormat))
				{
					Chart.SetMinX(nsMinX.Value);
					Chart.IsCursomAxisValue = true;
				}
				if (Chart.GetCurrentMaxX().ToString(nsMaxX.StringFormat) != nsMaxX.Value.ToString(nsMaxX.StringFormat))
				{
					Chart.SetMaxX(nsMaxX.Value);
					Chart.IsCursomAxisValue = true;
				}
			}
			if (Chart.GetCurrentMinY().ToString(nsMinY.StringFormat) != nsMinY.Value.ToString(nsMinY.StringFormat))
			{
				Chart.SetMinY(nsMinY.Value);
				Chart.IsCursomAxisValue = true;
			}
			if (Chart.GetCurrentMaxY().ToString(nsMaxY.StringFormat) != nsMaxY.Value.ToString(nsMaxY.StringFormat))
			{
				Chart.SetMaxY(nsMaxY.Value);
				Chart.IsCursomAxisValue = true;
			}
		}

		private void nsMinX_ValueChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			if (_Chart != null && base.IsLoaded && Chart.GetCurrentMinX().ToString(nsMinX.StringFormat) != nsMinX.Value.ToString(nsMinX.StringFormat))
			{
				if (nsMinX.Value > nsMaxX.Value)
				{
					nsMinX.Value = Chart.GetCurrentMinX();
					return;
				}
				Chart.SetMinX(nsMinX.Value);
				nsMinX.ValueChanged -= nsMinX_ValueChanged;
				nsMinX.Value = Chart.GetCurrentMinX();
				nsMinX.ValueChanged += nsMinX_ValueChanged;
				Chart.IsCursomAxisValue = true;
			}
		}

		private void nsMaxX_ValueChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			if (_Chart != null && base.IsLoaded && Chart.GetCurrentMaxX().ToString(nsMaxX.StringFormat) != nsMaxX.Value.ToString(nsMaxX.StringFormat))
			{
				if (nsMaxX.Value < nsMinX.Value)
				{
					nsMaxX.Value = Chart.GetCurrentMaxX();
					return;
				}
				Chart.SetMaxX(nsMaxX.Value);
				nsMaxX.ValueChanged -= nsMaxX_ValueChanged;
				nsMaxX.Value = Chart.GetCurrentMaxX();
				nsMaxX.ValueChanged += nsMaxX_ValueChanged;
				Chart.IsCursomAxisValue = true;
			}
		}

		private void nsMinY_ValueChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			if (_Chart != null && base.IsLoaded && Chart.GetCurrentMinY().ToString(nsMinY.StringFormat) != nsMinY.Value.ToString(nsMinY.StringFormat))
			{
				if (nsMinY.Value > nsMaxY.Value)
				{
					nsMinY.Value = Chart.GetCurrentMinY();
					return;
				}
				Chart.SetMinY(nsMinY.Value);
				nsMinY.ValueChanged -= nsMinY_ValueChanged;
				nsMinY.Value = Chart.GetCurrentMinY();
				nsMinY.ValueChanged += nsMinY_ValueChanged;
				Chart.IsCursomAxisValue = true;
				setNsYIncrement();
			}
		}

		private void nsMaxY_ValueChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			if (_Chart != null && base.IsLoaded && Chart.GetCurrentMaxY().ToString(nsMaxY.StringFormat) != nsMaxY.Value.ToString(nsMaxY.StringFormat))
			{
				if (nsMaxY.Value < nsMinY.Value)
				{
					nsMaxY.Value = Chart.GetCurrentMaxY();
					return;
				}
				Chart.SetMaxY(nsMaxY.Value);
				nsMaxY.ValueChanged -= nsMaxY_ValueChanged;
				nsMaxY.Value = Chart.GetCurrentMaxY();
				nsMaxY.ValueChanged += nsMaxY_ValueChanged;
				Chart.IsCursomAxisValue = true;
				setNsYIncrement();
			}
		}

		private void logMinY_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			nsMinY.Value = ((KeyValuePair<string, double>)logMinY.SelectedItem).Value;
			Chart.RefreshAxisYInterval();
			setComBoxContent();
		}

		private void logMaxY_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			nsMaxY.Value = ((KeyValuePair<string, double>)logMaxY.SelectedItem).Value;
			Chart.RefreshAxisYInterval();
			setComBoxContent();
		}

		private void timeMinX_ValueChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			if (_Chart != null && base.IsLoaded && Chart.GetCurrentMinX() != timeMinX.Value)
			{
				if (timeMaxX.Value < timeMinX.Value)
				{
					timeMinX.Value = Chart.GetCurrentMinX();
					timeMinX.setDate(new DateTime(1, 1, 1, 0, 0, 0).AddSeconds(_Chart.MinX).ToLongTimeString());
					return;
				}
				Chart.SetMinX(timeMinX.Value);
				timeMinX.ValueChanged -= timeMinX_ValueChanged;
				timeMinX.Value = Chart.GetCurrentMinX();
				timeMinX.setDate(new DateTime(1, 1, 1, 0, 0, 0).AddSeconds(Chart.GetCurrentMinX()).ToLongTimeString());
				timeMinX.ValueChanged += timeMinX_ValueChanged;
				Chart.IsCursomAxisValue = true;
			}
		}

		private void timeMaxX_ValueChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			if (_Chart != null && base.IsLoaded && Chart.GetCurrentMaxX() != timeMaxX.Value)
			{
				if (timeMaxX.Value < timeMinX.Value)
				{
					timeMaxX.Value = Chart.GetCurrentMaxX();
					timeMaxX.setDate(new DateTime(1, 1, 1, 0, 0, 0).AddSeconds(_Chart.MaxX).ToLongTimeString());
					return;
				}
				Chart.SetMaxX(timeMaxX.Value);
				timeMaxX.ValueChanged -= timeMaxX_ValueChanged;
				timeMaxX.Value = Chart.GetCurrentMaxX();
				timeMaxX.setDate(new DateTime(1, 1, 1, 0, 0, 0).AddSeconds(Chart.GetCurrentMaxX()).ToLongTimeString());
				timeMaxX.ValueChanged += timeMaxX_ValueChanged;
				Chart.IsCursomAxisValue = true;
			}
		}
	}
}
